package edu.ccut.saturn.studio.saturndata.editor;

import org.eclipse.jface.dialogs.IPageChangedListener;
import org.eclipse.jface.dialogs.PageChangedEvent;
import org.eclipse.ui.PartInitException;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
import org.w3c.dom.Document;


import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnXMLFormEditor;
import edu.ccut.saturn.studio.saturndata.model.SaturnDataModel;
import edu.ccut.saturn.studio.saturndata.page.DataItemPage;
import edu.ccut.saturn.studio.saturndata.page.OverViewPage;

/**
 * 数据项编辑器
 * 
 * @author 谷钰
 * 
 * @version 1.0 2008.12.09
 * 
 */
public class SaturnDataEditor extends SaturnXMLFormEditor {

	public static final String SATURN_DATA_EDITOR_KEY = SaturnDataEditor.class
			.getName();

	private OverViewPage overViewFormPage;
	private DataItemPage dataItemPage;

	private SaturnDataModel saturnDataModel;

	public SaturnDataModel getSaturnDataModel() {
		return saturnDataModel;
	}

	public SaturnDataEditor() {
		this.addPageChangedListener(new IPageChangedListener() {
			public void pageChanged(PageChangedEvent event) {
				if (event.getSelectedPage() instanceof OverViewPage) {
					SaturnProjectUtil.refreshProject(SaturnDataEditor.this
							.getEditorInput());
					overViewFormPage.onChangePage(SaturnDataEditor.this);
				}
			}
		});
	}

	protected void addFormPages() throws PartInitException {
		this.overViewFormPage = new OverViewPage(this);
		this.dataItemPage = new DataItemPage(this);

		super.addPage(0, this.overViewFormPage);
		super.addPage(1, this.dataItemPage);
	}

	protected void initModel() {
		StructuredTextEditor sourcePage = super.getTextEditor();
		saturnDataModel = new SaturnDataModel((Document) sourcePage
				.getAdapter(Document.class));
	}
}
